<?php
class admin_model_role extends admin_model_base  {
	public function __construct(){
		parent::__construct();
		$this->table = '_roles';
	}
	
	public function insert($role){
		$sql = 'insert ignore into '.$this->table.'(role) values(&role)';
		$params  = array('role'=>$role);
		return $this->admin_db->query($sql, $params);
	}
	
	public function selectList(){
		$sql = "select * from ".$this->table;
		return $this->admin_db->fetchAll($sql);
	}
	
	public function remove($id){
		$this->admin_db->query('delete from priv_roles where rid='.$id);
		$this->admin_db->query('delete from user_roles where rid='.$id);
		return $this->admin_db->remove($this->table, $id);
	}
	
	public function selectRolePriv($rid){
		$sql = "select concat(r.app, '_', r.controller, '_', r.action) resource from priv_roles p 
inner join _resources r on p.res_id=r.id 
where p.rid=&rid";
		$params = array('rid'=>$rid);
		return $this->admin_db->fetchAll($sql, $params);
	}
	
	public function select($id){
		$sql = "select * from ".$this->table. " where id=&id";
		$params = array('id'=>$id);
		return $this->admin_db->fetchRow($sql, $params);
	}
	
	public function savePriv($rid, $pid){
		$params = array('rid'=>$rid, 'res_id'=>$pid);
		return $this->admin_db->save('priv_roles', $params);
	}
	
	public function cleanPriv($rid){
		return $this->admin_db->query('delete from priv_roles where rid='.$rid);
	}
}